package exam;

import java.io.*;
import java.util.Scanner;

public class Example1_ReadWriteData {

    public static final String FILENAME = "D://info.txt";

    /**
     * 定义一个文件d:/info.txt，在文件中存储2行文本
     * 写一个程序，读取文件中的全部文本（使用BufferedReader）
     */

    public void writeData(int n) throws FileNotFoundException {

        PrintWriter pw = new PrintWriter(Example1_ReadWriteData.FILENAME);

        Scanner scanner = new Scanner(System.in);

        for (int i = 1; i <= n; i++) {
            System.out.println("请输入第"+i+"行文本内容");
            pw.println(scanner.nextLine());
            pw.flush();
        }

        pw.close();
    }

    public void readData() throws Exception {

        BufferedReader br = new BufferedReader(new FileReader(Example1_ReadWriteData.FILENAME));

        String temp;
        while((temp=br.readLine())!=null){
            System.out.println(temp);
        }
        br.close();
    }

    public static void main(String[] args) throws Exception {
        Example1_ReadWriteData data = new Example1_ReadWriteData();

        data.writeData(5);
        data.readData();
    }

}
